
package org.sql.commands.mysql.command;

// *************************************************************************/
// Charada Soft
// Area de Desarrollo de Sistemas de Información
// *************************************************************************/
// Proyecto : java connector sql 
// Archivo : Delete.java
// Descripcion : Clase Delete
// *************************************************************************/
// Version : 2.10.4
// Fecha de Creación: 13 de Julio de 2014
// Author : Daniel Andres Castillo Pedroza
// *************************************************************************/

import org.sql.commands.exception.DMLException;
import org.sql.commands.mysql.clause.Where;

public class Delete extends DML {
    
    // Atributos de la clase Delete
    
    private Where where;
    private String table;
    
    // Constructor de la clase Delete
    
    public Delete() {}
    
    // Metodos de la clase Delete
    
    /**
      * 
      * @param where 
    */

    public void setWhere(Where where) {
      this.where = where;
    }
    
    /**
      * 
      * @param table 
    */

    public void setTable(String table) {
      this.table = table;
    }
    
    // Metodos estáticos de la clase Delete
    
    /**
      * 
      * @param table
      * @return 
    */
    
    public static Delete creatorCommand(String table) {
      Delete delete = new Delete(); delete.setTable(table);
      
      return delete; // Retornando Objeto DELETE
    }
    
    /**
      * 
      * @param table
      * @param where
      * @return 
    */
    
    public static Delete creatorCommand(String table, Where where) {
      Delete delete = new Delete(); // Instanciando objeto
      delete.setTable(table); delete.setWhere(where);
      
      return delete; // Retornando Objeto DELETE
    }
    
    // Metodos sobreescritos de la clase Delete

    @Override
    public String getCommand() throws DMLException {
      if ((table == null) || (table.isEmpty())) {
        throw new DMLException(DMLException.TABLE_UNDEFINED);
      } // No ha establecido el Nombre de Tabla
      
      // Declarando y configurando dato para Proceso
      String commandDelete = "DELETE FROM " + table; 
      
      if (where != null) {
        commandDelete += " " + where.getClause();
      } // Estableciendo clausula de Actualización
      
      return commandDelete; // Retornando Comando DELETE
    }
}